/*
 * 数字化病案拍摄
 * SZH_BAFP
 */

//获取出院科室
$("#shoot_dept").combobox({
	url:ctx + "/MedicalShootManage/getDept",
	valueField:"ftykh",
	textField:"fksname",
	width:120, 
	height:24,
	onLoadSuccess : function() {
		$('#shoot_dept').combobox('select', 'TZYQY');
	}
});

$(function(){
	//初始化表格
	$("#ShootGrid").datagrid({
		url:'',
		toolbar:"#ShootBar",
		rownumbers:true,
		fitColumns:false, 
		//fitColumns : $(document.body).width()<1380? false : true,//根据屏幕的分辨率来对表格进行排版
		border : false,
		striped : true,
		fit : true,
		singleSelect: true,
		pagination : true,
		pageSize : 20,
		pageList : [ 20, 30,50 ],
		columns:[[
		        {field:'shoot',title:'操作',width:120,align:'center',
		        	formatter:function(value,row,index){
		        		if(row.fstatus == "1"){  //SZH_SHOOTRECORD --审批级别维护表  --审核状态 0 未审核，1 审核通过，2 审核未通过
		        			return "<button class='shootBtn ckShootBtn'  onclick='shoot_checkQuestion("+index+");'>查 看</button>";
		        		}else{
		        			return "<button class='shootBtn psShootBtn'  onclick='shoot("+index+");'>拍 摄</button>&nbsp;" +
		        			"<button class='shootBtn ckShootBtn'  onclick='shoot_checkQuestion("+index+");'>查 看</button>";
		        		}
		        	}
		        }, 
		        {field:'fprn',title:'病案号',width:120,align:'center',
		        	formatter:function(value,row,index){
		        		return value;
		        	}
		        },  
		        {field:'ftimes',title:'次数',width:50,align:'center' },  
		        {field:'fzyid',title:'住院ID',width:100,align:'center' },  
		        {field:'fname',title:'姓名',width:80,align:'center' },  
		        {field:'fsex',title:'性别',width:50,align:'center' },  
		        {field:'fbirthday',title:'出生日期',width:100,align:'center',
		        	formatter:function(value){
		        		return new Date(value).format("yyyy-MM-dd");
		        	}
		        },  
		        {field:'fbarcode',title:'条码号',width:160,align:'center' },  
		        {field:'fcydate',title:'出院日期',width:100,align:'center',
		        	formatter:function(value){
		        		return new Date(value).format("yyyy-MM-dd");
		        	}
		        },  
		        {field:'frydept',title:'入院科室',width:100,align:'center' },  
		        {field:'fcydept',title:'出院科室',width:100,align:'center' },  
		        /*{field:'ftpbh',title:'图片编号',width:100,align:'center',hidden:true },  */
		        {field:'frydate',title:'入院日期',width:100,align:'center',hidden:true,
		        	formatter:function(value){
		        		return new Date(value).format("yyyy-MM-dd");
		        	}	
		        }  
		       ]]
		});
	
	});

//点击检索按钮   加载数据表格
function shootGrid(){
	var shootDept = $("#shoot_dept").combobox("getValue"); //出院科室
	var shootDate1 = $("#shoot_date1").datebox("getValue"); //出院日期1
	var shootDate2 = $("#shoot_date2").datebox("getValue"); //出院日期2
	var shootName = $("#shoot_name").textbox("getText"); //姓名
	var shootFprn = $("#shoot_fprn").textbox("getText"); //病案号
	var shootMode = $("#mode").combobox("getValue"); //方式
	$("#ShootGrid").datagrid({
		url:ctx + '/MedicalShootManage/shootData',
		queryParams:{
			"shootDept":shootDept,
			"shootDate1":shootDate1,
			"shootDate2":shootDate2,
			"shootName":shootName,
			"shootFprn":shootFprn,
			"shootMode":shootMode
		},
		onLoadSuccess :function(data){
		    //根据权限设置判断页面按钮是否禁用
		    var tab = $("#tabs").tabs("getSelected");  //获取当前选择的面板
			var tabtext = tab.panel("options").title;  //获取当前选择面板的标题
			
			$.ajax({
				url : ctx + "/MedicalShootManage/getBtnPermision",
				dataType : 'json',
				type : 'post',
				data : {tabtext : tabtext},
				success : function(data){
					var pscount = 0;
					var ckcount = 0;
					if (data.length > 0) {
						for (var i = 0; i < data.length; i++) {
							if (data[i].FCONTENTRIGHT.charAt(0) == "1") {
								pscount ++ ;
							}
							if (data[i].FCONTENTRIGHT.charAt(1) == "1") {
								ckcount ++ ;
							}
						}
					}
					//判断该页面如果所有的权限设置拍摄按钮禁用的话执行的操作
					if (pscount == 0) {
						$('.psShootBtn').attr('disabled','disabled');
						$('.psShootBtn').removeClass('shootBtn');
						$('.psShootBtn').addClass('shShootBtn');
					}
					//判断该页面如果所有的权限设置查看按钮禁用的话执行的操作
					if (ckcount == 0) {
						$('.ckShootBtn').attr('disabled','disabled')
						$('.ckShootBtn').removeClass('shootBtn');
						$('.ckShootBtn').addClass('shShootBtn');
					}
				}
			})
		}
	}); 	
} 


//点击Grid的拍摄按钮   跳转到拍摄页面
function shoot(index){
	var rows = $("#ShootGrid").datagrid("getRows")[index]; 
	var fcydate = new Date(rows.fcydate).format("yyyy-MM-dd")
	$("#Shoot_Dialog").dialog({
		maximizable:true,
        closed: false,
        modal: true,
        href: ctx+'/MedicalShootManage/Shoot?tag=0&dialog=Shoot_Dialog&fprn='+rows.fprn + '&fzyid=' +rows.fzyid 
		+ '&ftimes='+rows.ftimes+ '&fname='+encodeURI(encodeURI(rows.fname))+ '&fcydate='+new Date(rows.fcydate).format("yyyy-MM-dd")
		+ '&fbarcode='+rows.fbarcode,
		title:'',// 拍摄
		onClose : function(){
			Capture.CloseDeviceEx();
		} 
			
	});
	//获取树节点
	$("#shootTree").tree({    
	    url: ctx + "/MedicalShootManage/tree",  
	    animate: true,
	    checkbox:false,
	    queryParams:{
	    	"fzyid":rows.fzyid
	    },
	    onSelect:function(node){  
	    	var parent = $("#shootTree").tree("getParent",node.target);
	    	//当选中第几页时，将所属分类的下标保存下来
	    	if(node.chlidren != undefined){
	    		var index = $("#shootTree .tree-node-selected").parent().parent().parent().index();
	    		$("#currentIndex").html(index);
	    	}else if(parent){
	    		var idx = $("#shootTree .tree-node-selected").parent().index();
	    		$("#currentIndex").html(idx);
	    	}
	    	//previewImg();   //浏览图片
	    },
	    /*onDblClick : function(node){
	    	//双击浏览图片
	    	previewImg();  
	    },*/
		onContextMenu:function(e,node){  //右键点击节点并显示快捷菜单
			/*e.preventDefault()
			$("#shootTree").tree('select',node.target);
			//document.getElementById("shootBG").style.display ="none";  
		  //  document.getElementById("shootShow").style.display ="none";
			 $(".slide_container").css("display","none");
		     $("#camero").css('display','block');
			//显示快捷菜单	
			$("#rightMenu").menu('show',{
				left: e.pageX,
				top: e.pageY
			});*/
		}
	}); 
	$("#fprn").html(rows.fprn);
	$("#fzyid").html(rows.fzyid);
	$("#ftimes").html(rows.ftimes);
	$("#fname").html(rows.fname);
	$("#shootFcydate").html(new Date(rows.fcydate).format("yyyy-MM-dd"));
	$("#shootFbarcode").html(rows.fbarcode);
	
}

//跳转问题页  点击查看按钮
function shoot_checkQuestion(index){
	var row = $("#ShootGrid").datagrid("getRows")[index];
	$('#shootTabs').tabs('select', '问题页浏览');
	//加载表格
	$("#QuestionPreviewGrid").datagrid({
		url:ctx + '/MedicalShootManage/questionData',
		queryParams:{
			"fprn" : row.fprn,
			"ftimes" : row.ftimes ,
			"fzyid" : row.fzyid
		},
		toolbar:"",
		fitColumns:true,
		border : false,
		striped : true,
		fit : true,
		singleSelect: false,
		pagination : true,
		pageSize : 20,
		pageList : [ 20, 30,50 ],
		columns:[[
		        {field:'FBARCODE',title:'条码号',width:100,align:'center' },  
		        {field:'FPRN',title:'病案号',width:100,align:'center' },  
		        {field:'FTIMES',title:'次数',width:60,align:'center' },  
		        {field:'FFLFZBH',title:'图片分类编号',width:100,align:'center' },  
		        {field:'FPAGECODE',title:'图片页码',width:60,align:'center',
		        	formatter:function(value,row,index){
		        		if(value != null){
		        			return "第"+value+"页"
		        		}else{
		        			return "";
		        		}
		        	}
		        },  
		        {field:'FTPBH',title:'图片编号',width:100,align:'center' },  
		        {field:'FSBUSER',title:'上报人',width:100,align:'center' },  
		        {field:'FSBDATE',title:'上报时间',width:100,align:'center' },  
		        {field:'FSTATE',title:'处理状态',width:100,align:'center' ,
		        	formatter:function(value,row,index){
		        		if(value =="0"){
		        			return "<span style='color:#DC4B3F'>未处理<span>"
		        		}else{
		        			return "<span style='color:#18A15F'>已处理<span>"
		        		}
		        	}
		        },  
		        {field:'FCLUSER',title:'处理人',width:100,align:'center' },  
		        {field:'FCLYJ',title:'处理意见',width:100,align:'center' },  
		        {field:'FCLDATE',title:'处理时间',width:100,align:'center' },  
		        {field:'FREASON',title:'错误内容',width:100,align:'center' },  
		        {field:'FREMARK',title:'备注',width:150,align:'center' }
		  ]]
	});

}

//打开dialog  未用
function openDialog(){
	$("#shootDialog").dialog({
		maximizable:true,
        closed: false,
        modal: true,
		title: "水印设置",
		buttons:[{
			text:"关闭",
			handler:function(){
				$('#shootDialog').dialog("close");
			}
		},{
			text:"确认",
			handler:function(){
				
			}
		}
		]
	});
}

//关闭遮盖层  未用
/*function closedShoot(){ 
	//document.getElementById("shootBG").style.display ="none";  
    //document.getElementById("shootShow").style.display ="none";  
    $(".slide_container").css("display","none");
    $("#camero").css('display','block');
}
*/   

//图片播放效果  未用
function slider(){	
  $("#shootSlider").responsiveSlides({
	    auto: false,
	    pager: false,
	    nav: true,
	    speed: 500,
	    // 对应外层div的class : slide_container
	    namespace: "slide"
	    });
} 

//打开审核未通过dialog  未用
function openShootDialog(index){
	
	$("#auditDialog").dialog({
		maximizable:true,
        closed: false,
        modal: true,
		title: "审核未通过原因",
		buttons:[{
			text:"关闭",
			handler:function(){
				$('#auditDialog').dialog("close");
			}
		}
		]
	});
	var itmes = $("#ShootGrid").datagrid("getRows")[index];
	//初始化表格
	$("#statuGrid").datagrid({
		url:ctx + '/MedicalShootManage/reason',
		queryParams:{
			"fprn" : itmes.fprn,
			"ftimes" : itmes.ftimes,
			"fzyid" : itmes.fzyid,
			"number" : itmes.ftpbh
		},
		fitColumns:true,
		rownumbers:true,
		border : false,
		striped : true,
		fit : true,
		singleSelect: false,
		columns:[[
		        {field:'opreat',title:'',width:50,align:'center',
		        	formatter:function(value,row,index){
		        		return "<a href='#' class='easyui-linkbutton' iconCls='icon-search' onclick='openImgPreview("+index+")'>查看</a>";
		        	}
		        },
		        {field:'FID',title:'原因编号',width:50,align:'center'},
		        {field:'FREASON',title:'原因',width:100,align:'center',
		        	formatter:function(value,row,index){
		        		return "第"+row.ftpym+"页&nbsp;&nbsp;"+value;
		        	}
		        },
		        {field:'FPRN',title:'病案号',width:100,align:'center',hidden:true},
		        {field:'FTIMES',title:'次数',width:100,align:'center',hidden:true},
		        {field:'FBARCODE',title:'条形码',width:100,align:'center',hidden:true},
		        {field:'FFLFZBH',title:'图片分类',width:100,align:'center',hidden:true},
		        {field:'FTPBH',title:'图片编号',width:100,align:'center',hidden:true},
		        {field:'fbasecode',title:'base64编码',width:100,align:'center',hidden:true},
		        {field:'ftpym',title:'页码',width:100,align:'center',hidden:true}
		]]
	});
};

var fcode =$("#fcode").text(); //获取当前用户code

//拍摄页面-审核对话框  未用
function open_audit_Dialog(){
	var node = $("#shootTree").tree("getSelected");
	if(!node){
		$.messager.alert('提示','请选中需要审核的图片！','info');
		return;
	}
	$("#camero").css('display','none');
	$("#Shoot_MessageDialog").dialog({
		maximizable:true,
        closed: false,
        modal: true,
		title: "审核通过",
		buttons:[{
			text:"确认",
			handler:function(){
				//审核通过
					var number = $("#shootSlider").find('li[class*=on]>div>.auditImgNumber').text();//图片编码
					var categoryNumber = $("#shootSlider").find('li[class*=on]>div>.imgCategory').text();//图片分类编码
					var path = $("#shootSlider").find('li[class*=on]>div>.imgPath').text();//图片路径
					var id = node.id; //获取图片ID	
					id = id.substring(0,laseIndexOf(","));
   				    $.ajax({
						url:ctx + "/MedicalShootManage/auditOperation",
						dataType:'json',
						type:'post',
						data:{
							"path" : path,
							"number" : number,
							"categoryNumber" : categoryNumber,
							"id" : id,
							"fprn" : $(".shoot_dialog_fprn").text(),//病案号
							"fname" : $(".shoot_dialog_fname").text(), //姓名
							"fzyid" : $(".shoot_dialog_fzyid").text(), //住院ID
							"ftimes" : $(".shoot_dialog_ftimes").text(),//次数
							"barcode":$("#shootFbarcode").text()  //条形码
						},
						success:function(data){
							if(data.msg == true){
								alert("审核成功！");
								$("#messageDialog").dialog("close");
							}else if(data.msg == "error"){
								alert("当前图片已审核过，请重新操作！");
								$("#messageDialog").dialog("close");
							}else{
								alert("审核操作失败,请联系管理员！");
								$("#messageDialog").dialog("close");
							}
						},
						error:function(){
							alert("审核操作失败,请联系管理员！");
							$("#messageDialog").dialog("close");
						}
					});
			}
		},{
			text:"关闭",
			handler:function(){
				$('#Shoot_MessageDialog').dialog("close");
				$("#camero").css('display','');
			}
		}
		]
	});
	$(".shoot_dialog_fprn").html($("#fprn").text());
	$(".shoot_dialog_ftimes").html($("#ftimes").text());
	$(".shoot_dialog_fzyid").html($("#fzyid").text());
	$(".shoot_dialog_fname").html($("#fname").text());
	$(".shoot_dialog_fcydate").html($("#shootFcydate").text());
	
}



